package com.medicine.web.controller.manage;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.medicine.manage.domain.Order;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.service.IOrderProcessService;
import com.medicine.manage.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.annotation.Log;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.enums.BusinessType;
import com.medicine.manage.domain.Packaging;
import com.medicine.manage.service.IPackagingService;
import com.medicine.common.utils.poi.ExcelUtil;
import com.medicine.common.core.page.TableDataInfo;

/**
 * 装袋打包Controller
 *
 * @author admin
 * @date 2024-10-13
 */
@RestController
@RequestMapping("/manage/packaging")
public class PackagingController extends BaseController
{
    @Autowired
    private IPackagingService packagingService;

    @Autowired
    private IOrderProcessService orderProcessService;

    @Autowired
    private IOrderService orderService;

    private final Long TYPE = 4L;

    /**
     * 查询装袋打包列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Packaging packaging)
    {
        startPage();
        List<Packaging> list = packagingService.selectPackagingList(packaging);
        return getDataTable(list);
    }

    /**
     * 导出装袋打包列表
     */
    @Log(title = "装袋打包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Packaging packaging)
    {
        List<Packaging> list = packagingService.selectPackagingList(packaging);
        ExcelUtil<Packaging> util = new ExcelUtil<Packaging>(Packaging.class);
        util.exportExcel(response, list, "装袋打包数据");
    }

    /**
     * 获取装袋打包详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(packagingService.selectPackagingById(id));
    }

    /**
     * 新增装袋打包
     */
    @Log(title = "装袋打包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Packaging packaging)
    {
        OrderProcess orderProcess = orderProcessService.selectOrderProcessByOrderAndType(packaging.getOrderId(), TYPE);
        if(orderProcess!=null){
            packaging.setProcessId(orderProcess.getId());
            orderProcess.setStartTime(new Date());
            orderProcessService.updateOrderProcess(orderProcess);
        }

        Order order = new Order();
        order.setId(packaging.getOrderId());
        order.setStatus(TYPE);
        orderService.updateOrder(order);

        return toAjax(packagingService.insertPackaging(packaging));
    }

    /**
     * 修改装袋打包
     */
    @Log(title = "装袋打包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Packaging packaging)
    {
        return toAjax(packagingService.updatePackaging(packaging));
    }

    /**
     * 删除装袋打包
     */
    @Log(title = "装袋打包", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(packagingService.deletePackagingByIds(ids));
    }
}
